<template>
  <div id="con">
    <!-- 头部导航 -->
    <van-nav-bar class="top" fixed placeholder title="确认订单">
      <template #left>
        <van-icon
          color="#666"
          name="arrow-left"
          size="20"
          @click="$router.back()"
        />
      </template>
    </van-nav-bar>
    <div class="address" @click="goto">
      <p class="p1"><van-icon name="map-marked" size="18" />收货地址</p>
      <p class="p2">{{ uaddress.address }}</p>
      <div>
        <span>{{ uaddress.name }}</span
        ><span>{{ uaddress.tel }}</span>
      </div>
    </div>
    <div class="cartlist">
      <van-card
        v-for="item in surelist"
        :key="item.goods_id"
        :num="item.goods_num"
        :price="item.goods_price"
        :title="item.goods_name"
        :thumb="item.goods_small_logo"
      />
    </div>
    <van-submit-bar
      :price="totalprice"
      button-text="提交订单"
      @submit="onSubmit"
    />
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  name: "Confirmorder",
  data() {
    return {
      uaddress: "",
      surelist: [],
    };
  },
  async created() {
    const ulist = this.$route.params.val || {};
    this.uaddress = ulist;
    const lists = this.$route.params.cartlist || [];
    this.surelist = lists;
  },
  methods: {
    goto() {
      this.$router.push({
        name: "Address",
        params: {
          cartlist: this.surelist,
        },
      });
    },
    async onSubmit() {
      if (this.uaddress.address) {
        const { data } = await this.$request.post(
          "/api/orders/0",
          {
            data: this.surelist,
            user_id: this.userinfo.user_id,
            sjr: this.uaddress.name,
            sjdz: this.uaddress.address,
            total_money: parseFloat(this.totalprice / 100).toFixed(2),
            tel: this.uaddress.tel,
          },
          {
            headers: {
              authorization: "auth_token",
            },
          }
        );
        if (data.code === 1) {
          const { data } = await this.$request.put(
            "/api/orders/" + this.userinfo.user_id,
            {
              status: "已支付",
            },
            {
              headers: {
                authorization: this.userinfo.authorization,
              },
            }
          );
          if (data.code === 1) {
            this.$router.push("/orders");
            // 清空所选的购物车             
          } else {
            this.$toast("提交订单失败，请重试");
            return;
          }
        }
      } else {
        this.$toast("请填写收货地址");
        return;
      }
    },
  },
  computed: {
    ...mapState("user", ["userinfo"]),
    totalprice() {
      return (
        100 *
        this.surelist.reduce((pre, item) => {
          return pre + item.goods_price * item.goods_num;
        }, 0)
      );
    },
  },
};
</script>
<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
#con {
  padding: 10px;
  box-sizing: border-box;
  background-color: #eee;
  height: 100%;
}
.address {
  width: 100%;
  background-color: #fff;
  border-radius: 10px;
  color: #232323;
  padding: 20px;
  box-sizing: border-box;
  text-align: left;
  .p1 {
    font-size: 18px;
  }
  .p2 {
    font-size: 16px;
  }
  div {
    span {
      font-size: 15px;
      margin-right: 10px;
    }
  }
}
.cartlist {
  margin-top: 10px;
  width: 100%;
  text-align: left;
  .van-card {
    border-radius: 10px;
    background-color: #fff;
  }
}
.van-address-list {
  position: absolute;
  z-index: 99;
}
</style>